package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    //分类表
    @Autowired
    private CategoryService categoryService;


    // DishController定义方法新增菜品

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    @GetMapping
    public R<Page> page(int page, int pageSize, String name) {

        Page<Dish> page1 = new Page<>();
        //new 新对象 里面的属性没有赋值 ,但是page1经过分页查询,已经有值所以拷贝到page2就可以
        Page<DishDto> page2 = new Page<>();

        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //过滤
        qw.like(name != null, Dish::getName, name);
        //排序
        qw.orderByDesc(Dish::getUpdateTime);
        //执行
        dishService.page(page1, qw);
        //拷贝,除了records,因为records对应的List集合,就是页面上展示的数据所承载的集合,所以加入第三个属性忽略records
        BeanUtils.copyProperties(page1, page2, "records");
        //从page1获取records的list集合,通过stream流处理过后传到page2中 (map把每个元素拿出来)
        //it代表的是从records中获取的每一个菜品
        //获取菜品的id,通过id去查分类表,获取分类的对象,再用分类对象getname获取分类名称
        //最终需要的是一个dto对象,dto对象的扩展属性已经添加完成,但是继承自dish的基本属性都是空的,所以通过it将菜品的基础属性拷贝给新的dto对象
        //最后吧dto对象收集转成list集合
        //这一整个拉姆达表达式就是我们需要的结果,最后赋给 List<DishDto> list
        List<Dish> records = page1.getRecords();

        List<DishDto> list = records.stream().map((it) -> {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(it, dishDto);

            Long categoryId = it.getCategoryId();

            Category category = categoryService.getById(categoryId);

            String categoryname = category.getName();

            dishDto.setCategoryName(categoryname);
            return dishDto;

        }).collect(Collectors.toList());


        page2.setRecords(list);


        return R.success(page2);

    }

    //使用dto泛型,因为继承了dish 扩展了dishflavor口味属性
    //跟页面对应,将数据返回给页面
    //id在请求url里,所以加@Path
    //根据id查菜品和口味信息
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id) {

        DishDto dishDto = dishService.getByIdWithFlavor(id);


        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.UpdateWithFlavor(dishDto);
        return R.success("新增成功");

    }

//    @GetMapping("/list")
//    public R<List<Dish>> list(Dish dish) {
//        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
//        //条件构造
//        qw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
//        qw.eq(Dish::getStatus,1);
//        //排序
//        qw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//        List<Dish> list = dishService.list(qw);
//        return R.success(list);
//    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //条件构造
        qw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        qw.eq(Dish::getStatus,1);
        //排序
        qw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);


        List<Dish> list = dishService.list(qw);
      List <DishDto>  dss=list.stream().map((it)->{
                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(it,dishDto);
                Long id = it.getCategoryId();
                Category category = categoryService.getById(id);

                if(category!=null){
                    String name = category.getName();
                    dishDto.setCategoryName(name);
                }

                //当前菜品id
                Long id1 = it.getId();
                LambdaQueryWrapper<DishFlavor> qww = new LambdaQueryWrapper<>();

                qww.eq(DishFlavor::getDishId,id1);

                List<DishFlavor> list1 = dishFlavorService.list(qww);

                dishDto.setFlavors(list1);


                return dishDto;
            }).collect(Collectors.toList());


        return R.success(dss);
    }


}
